package com.roads.engineering.controller;

import com.roads.common.constant.Constants;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.utils.SecurityUtils;
import com.roads.common.utils.StringUtils;
import com.roads.engineering.domain.summary.SummaryRecordUserVo;
import com.roads.engineering.service.IReportService;
import com.roads.engineering.service.ISummaryService;
import com.roads.project.domain.ProInstitution;
import com.roads.project.domain.ProPeriod;
import com.roads.project.domain.ProProject;
import com.roads.project.domain.ProReport;
import com.roads.project.domain.bid.BidSelect;
import com.roads.project.service.IBidService;
import com.roads.project.service.IProInstitutionService;
import com.roads.project.service.IProPeriodService;
import com.roads.project.service.IProProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ReportController
 * @Description 报表controller
 * @Author nhp
 * @Date 2022/6/10 0010 15:30
 * @Version 1.0.0
 **/
@RestController
@RequestMapping("/engineering/report")
public class ReportController extends BaseController {

    @Autowired
    private IBidService bidService;

    @Autowired
    private IProPeriodService periodService;

    @Autowired
    private IReportService reportService;

    @Autowired
    private IProInstitutionService institutionService;

    @Autowired
    private ISummaryService summaryService;

    @Autowired
    private IProProjectService projectService;

    /**
     * 初始化报表页面
     *
     * @param reportType 报表类型
     * @return 报表集合
     */
    @GetMapping("/init")
    public AjaxResult init(String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean isAdmin = SecurityUtils.isAdmin(getUserId());
        // 初始化标段
        List<BidSelect> bids = bidService.selectProjectBidBranch(getUsername(), isAdmin);
        ajaxResult.put("bids", bids);
        if (bids.size() > 0) {
            String institutionCode = bids.get(0).getBids().get(0).getCode();
            ajaxResult.put("institutionCode", institutionCode);
            ProPeriod search = new ProPeriod();
            search.setInstitutionCode(institutionCode);
            // 初始化工期
            List<ProPeriod> periods = periodService.selectModelList(search);
            ajaxResult.put("periods", periods);
            if (periods.size() > 0) {
                //获取当前计量期次
                String periodName = periods.get(0).getPeriodName();
                ProPeriod currentPeriod = periodService.getCurrentPeriod(institutionCode);
                if (StringUtils.isNotNull(currentPeriod)) {
                    periodName = currentPeriod.getPeriodName();
                }
                // 初始化汇总用户
                List<SummaryRecordUserVo> users = summaryService.selectSummaryRecordUserList(institutionCode, periodName);
                if (users.size() > 0) {
                    ajaxResult.put("users", users);
                    ajaxResult.put("user", users.get(0).getUser());
                    ajaxResult.put("periodName", periodName);
                } else {
                    // 如果当前计量工期没有汇总用户，则需要将工期设置为上一期，第一期不进行向上处理
                    String lastPeriodName = String.valueOf(Integer.parseInt(periodName) - 1);
                    if (Constants.FIRST_PERIOD.equals(periodName)) {
                        lastPeriodName = Constants.FIRST_PERIOD;
                    }
                    ajaxResult.put("periodName", lastPeriodName);
                    users = summaryService.selectSummaryRecordUserList(institutionCode, lastPeriodName);
                    if (users.size() > 0) {
                        ajaxResult.put("users", users);
                        ajaxResult.put("user", users.get(0).getUser());
                    }
                }
            }

            // 初始化报表列表
            long userId = 0L;
            ProInstitution institution = institutionService.getModelByInnerCode(institutionCode);
            if (!SecurityUtils.isAdmin(getUserId())) {
                userId = getUserId();
            }
            List<ProReport> list = reportService.selectReportList(institution.getProjectId(), userId, reportType);
            ajaxResult.put("list", list);
        }
        return ajaxResult;
    }

    /**
     * 初始化项目报表页面
     *
     * @param reportType 报表类型
     * @return 报表集合
     */
    @GetMapping("/project/init")
    public AjaxResult projectInit(String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        ProProject proProject = new ProProject();
        proProject.setCreateBy(getUsername());
        List<ProProject> projects;
        if (SecurityUtils.isAdmin(getUserId())) {
            projects = projectService.selectProProjectListAll(proProject);
        } else {
            projects = projectService.selectProProjectList(proProject);
        }
        ajaxResult.put("projects", projects);
        if (projects.size() > 0) {
            int projectId = projects.get(0).getProjectId();
            ajaxResult.put("projectId", projectId);
            // 初始化工期
            List<ProPeriod> periods = periodService.selectPeriodNameListByProjectId(projectId);
            ajaxResult.put("periods", periods);
            if (periods.size() > 0) {
                //获取当前计量期次
                String periodName = periods.get(0).getPeriodName();
                ProPeriod currentPeriod = periodService.getCurrentPeriodByProjectId(projectId);
                if (StringUtils.isNotNull(currentPeriod)) {
                    periodName = currentPeriod.getPeriodName();
                }
                ajaxResult.put("periodName", periodName);
            }
            // 初始化报表列表
            long userId = 0L;
            if (!SecurityUtils.isAdmin(getUserId())) {
                userId = getUserId();
            }
            List<ProReport> list = reportService.selectReportList(projectId, userId, reportType);
            ajaxResult.put("list", list);
        }
        return ajaxResult;
    }

    /**
     * 初始化报表视图页面
     *
     * @param institutionCode 机构编号
     * @param periodName 工期
     * @param user 用户
     * @param reportType 报表类型
     * @return 机构、工期、用户集合
     */
    @GetMapping("/initView")
    public AjaxResult initView(String institutionCode, String periodName, String user, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean isAdmin = SecurityUtils.isAdmin(getUserId());
        // 初始化标段
        List<BidSelect> bids = bidService.selectProjectBidBranch(getUsername(), isAdmin);
        ajaxResult.put("bids", bids);
        if (bids.size() > 0) {
            ajaxResult.put("institutionCode", institutionCode);
            ProPeriod search = new ProPeriod();
            search.setInstitutionCode(institutionCode);
            // 初始化工期
            List<ProPeriod> periods = periodService.selectModelList(search);
            ajaxResult.put("periods", periods);
            if (periods.size() > 0) {
                //直接从参数获取期次
                ajaxResult.put("periodName", periodName);
                // 初始化汇总用户
                List<SummaryRecordUserVo> users = summaryService.selectSummaryRecordUserList(institutionCode, periodName);
                if (users.size() > 0) {
                    ajaxResult.put("users", users);
                    ajaxResult.put("user", user);
                }
            }
        }
        return ajaxResult;
    }

    /**
     * 初始化项目报表视图页面
     *
     * @param projectId 项目ID
     * @param periodName 工期
     * @param reportType 报表类型
     * @return 项目、工期集合
     */
    @GetMapping("/initProjectView")
    public AjaxResult initProjectView(Integer projectId, String periodName, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        List<ProProject> projects = new ArrayList<>();
        ProProject project = projectService.selectProProjectByProjectId(projectId);
        projects.add(project);
        ajaxResult.put("projects", projects);
        ajaxResult.put("projectId", projectId);
        List<ProPeriod> periods = periodService.selectPeriodNameListByProjectId(projectId);
        ajaxResult.put("periods", periods);
        ajaxResult.put("periodName", periodName);
        return ajaxResult;
    }

    /**
     * 获取报表列表
     *
     * @param reportType 报表类型
     * @param institutionCode 机构编号
     * @return 报表集合
     */
    @GetMapping("/getReportList")
    public AjaxResult getReportList(String institutionCode, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        ProInstitution institution = institutionService.getModelByInnerCode(institutionCode);
        long userId = 0L;
        if (!SecurityUtils.isAdmin(getUserId())) {
            userId = getUserId();
        }
        List<ProReport> list = reportService.selectReportList(institution.getProjectId(), userId, reportType);
        ajaxResult.put("list", list);
        return ajaxResult;
    }

    /**
     * 获取项目报表列表
     *
     * @param reportType 报表类型
     * @param projectId 项目ID
     * @return 报表集合
     */
    @GetMapping("/getReportListByProjectId")
    public AjaxResult getReportListByProjectId(Integer projectId, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        long userId = 0L;
        if (!SecurityUtils.isAdmin(getUserId())) {
            userId = getUserId();
        }
        List<ProReport> list = reportService.selectReportList(projectId, userId, reportType);
        ajaxResult.put("list", list);
        return ajaxResult;
    }

}
